package com.shfl.task.logic.api;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.shfl.task.common.ApiResultPagingHelper;
import com.shfl.task.common.RequestHelper;
import com.shfl.task.common.SystemConstant;
import com.shfl.task.logic.constants.CommonConstants;
import com.shfl.task.logic.model.response.TaskLogResponse;
import com.shfl.task.logic.service.TaskLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/api/task/log")
public class TaskLogController {

    @Autowired
    private TaskLogService taskLogService;

    @GetMapping(value = "/page/list")
    public ApiResultPagingHelper<TaskLogResponse, Void> page(
            RequestHelper requestHelper
    ) {
        requestHelper.apply();
        Map<String, Object> objectMap = requestHelper.getKeyMap();
        PageInfo<TaskLogResponse> pageInfo = taskLogService.findPage(requestHelper.getPageable(), objectMap);
        if (pageInfo.getTotal() > 0) {
            pageInfo.getList().forEach(item -> {
                item.setTaskStatusValue(CommonConstants.TaskLogStatus.getName(item.getTaskStatus()));
                item.setLastTimeValue(DateUtil.format(new Date(item.getLastTime()), DatePattern.NORM_DATETIME_PATTERN));
                item.setCreateTimeValue(DateUtil.format(new Date(item.getCreateTime()), DatePattern.NORM_DATETIME_PATTERN));
            });
        }
        return new ApiResultPagingHelper<>(SystemConstant.RE_SUCCESS_CODE,
                pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum(),
                pageInfo.getList());
    }


}
